<?php

namespace App\Http\Controllers\Goods;

use App\Http\Controllers\Controller;
use App\Server\GoodsCateService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class GoodsCategoryController extends Controller
{

    /**
     * 商品分类展示
     * @uses 李耀
     * POST
     * www.test.com/goodsCategoryPage
     *{"code":200,"data":[{"current_page":1,"data":[{"id":1,"pid":0,"cate_name":"\u624b\u673a","sort":50,"is_show":1,"is_hot":0,"create_time":"2023-04-25 19:34:20","update_time":"2023-04-25 19:34:30","delete_time":null}],"first_page_url":"http:\/\/www.test.com\/goodsCategoryPage?page=1","from":1,"last_page":1,"last_page_url":"http:\/\/www.test.com\/goodsCategoryPage?page=1","links":[{"url":null,"label":"&laquo; Previous","active":false},{"url":"http:\/\/www.test.com\/goodsCategoryPage?page=1","label":"1","active":true},{"url":null,"label":"Next &raquo;","active":false}],"next_page_url":null,"path":"http:\/\/www.test.com\/goodsCategoryPage","per_page":3,"prev_page_url":null,"to":1,"total":1}]}
     * @param Request $request
     */
    public function goodsCategoryPage(Request $request){
        $cate_name=$request->get('cate_name');
        $data=GoodsCateService::goodsCateList($cate_name);
        return response()->json(['code'=>200,'data'=>[$data]]);
    }

    /**
     * @uses 李耀
     * GET
     * 商品分类展示页面
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\think\response\View
     */
    public function goodsCategoryAdd(){
        return view('goods.index');
    }

    /**
     * 商品分类添加
     * @uses 李耀
     * POST
     * www.test.com/goodsCategoryAdd
     * {"code":200,"msg":"Ok","data":[]}
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function goodsCategoryAll(Request $request){
        try {
            $param=$request->all();
    //        $request->validate([
    //            'cate_name'=>'required',
    //            'spec'=>'required|array',
    //            'attr'=>'required|array',
    //        ],[
    //            'cate_name.required'=>'分类名称不能为空',
    //            'spec.required'=>'规格信息不能为空',
    //            'spec.array'=>'规格信息必须是数组',
    //            'attr.required'=>'属性信息不能为空',
    //            'attr.array'=>'属性信息必须是数组',
    //        ]);
            if (!$param){
                return response()->json(['code'=>2001,'msg'=>'error','data'=>[]]);
            }
            //验证商品规格
            $param=[
                'cate_name'=>'手机',
                'spec'=>[
//                    ['name' => '  ', 'sort' => 1, 'value' => [' ', ' ']],//过滤掉的数据
                    ['name' => '颜色', 'sort' => 1, 'value' => ['白色', '黑色']],
//                    ['name' => ' ', 'sort' => 1, 'value' => ['  ', '  ']],//过滤掉的数据
                    ['name' => '内存', 'sort' => 1, 'value' => ['32G', '256G']],
                ],
                'attr'=>[
//                    ['name' => '  ', 'sort' => 1, 'value' => ['  ', '  ']],//过滤掉的数据
                    ['name' => '产地', 'sort' => 1, 'value' => ['中国大陆', '上海']],
                    ['name' => '重量', 'sort' => 1, 'value' => ['250g']],
//                    ['name' => '  ', 'sort' => 1, 'value' => ['200g', '']],//过滤掉的数据
                ],
            ];
            # 先过滤规格
            $param=GoodsCateService::data_filter($param,'attr');
            GoodsCateService::save($param);
            return response()->json(['code'=>200,'msg'=>'Ok','data'=>[]]);
        } catch (\Exception $exception){
            return response()->json(['code'=>2001,'msg'=>$exception->getMessage(),'data'=>[]]);
        }
    }

    /**
     * 商品分类详情
     * @uses 李耀
     * POST
     * www.test.com/goodsCategoryAdd
     * ###
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function goodsCategoryRead(Request $request){
        try {
            $id=$request->get('id');
            $info=GoodsCateService::detail($id);
            return response()->json(['code'=>200,'data'=>[$info]]);
        } catch (\Exception $exception){
            return response()->json(['code'=>200,'msg'=>$exception->getMessage(),'data'=>[]]);
        }
    }

}
